$(function () {
    var $datagrid = $("#datagrid"), $toolbar = $("#toolbar"), $dialog = $("#dialog"), $pickup = $("#pickup");
    $("#saveScenic").on("click", function () {
        openDialog();
    });
    $("#editScenic").on("click", function () {
        var rows = $datagrid.datagrid("getSelected");
        console.info(rows);
        if (rows) {
            var url = "form.jsp?scenicName=" + rows.ScenicName + "&cityId=" + rows.CityId + "&longitude=" + rows.Longitude
                + "&latitude=" + rows.Latitude + "&position=" + rows.Position
                + "&description=" + rows.Description + "&scenicId=" + rows.Id ;
            openDialog(url);
        }
    });

    $pickup.window()

    $datagrid.datagrid({
        singleSelect: true,
        fit: true,
        border: false,
        fitColumns: true,
        url: Constant.ctx("/mulanweb/admin/scenic_spot/search"),
        method: 'post',
        pagination: true,
        toolbar: $toolbar,
        columns: [[
            {field: 'Id', title: '编号'},
            {field: 'ScenicName', title: '名称'},
            {field: 'Longitude', title: '经度'},
            {field: 'Latitude', title: '纬度'},
            {field: 'CreateTime', title: '注册时间'}
        ]],
        onBeforeLoad: function (param) {
            param.PageNumber = param.page;
            param.PageSize = param.rows;

            var spotName = $("#spotName").val();
            if(spotName){
                param.name = spotName;
            }
            return true;
        },
        loadFilter: function (data) {
            var newData = {};
            if (data.ReturnCode == 1) {
                newData.rows = data.DataList;
                if (newData.rows.length > 0) {
                    newData.total = data.TotalRow;
                } else {
                    newData.total = 0;
                }
            }
            return newData;
        }
    });
    function saveAccount() {
        $("form").form("submit", {
            url: $("form").attr("action"),
            onSubmit: function () {
                var isValid = $(this).form('validate');
                if (!isValid) {
                    $.messager.progress('close');	// hide progress bar while the form is invalid
                }
                return isValid;
            },
            success: function (respText) {
                var resp = $.parseJSON(respText);
                console.info(respText);
                console.info("respText.ReturnCode=" + respText.ReturnCode);
                console.info("resp=" + resp.ReturnCode);
                Constant.showMessage(resp.AlertMessage, respText.AlertMessage, "操作成功");
                if (resp.ReturnCode == 1) {
                    $("#datagrid").datagrid("reload");
                    $dialog.dialog("close");
                }
            }
        });
    }

    function openDialog(url) {
        $dialog.dialog({
            title: url?"编辑景点":"新增景点",
            width: 400,
            height: 500,
            closed: false,
            cache: false,
            href: url || 'form.jsp',
            modal: true,
            onClose: function () {
                $("#form-dialog").dialog("destroy");
            },
            buttons: [{
                text: '保存',
                handler: saveAccount
            }]
        });
    }
})

function doSearch(){
    $("#datagrid").datagrid("reload");
}